package com.jhws.business.comm.service;

import com.jhws.business.comm.bo.Room;
import com.jhws.business.comm.bo.RoomBo;
import com.jhws.business.comm.bo.TblBuildingDimBo;
import com.jhws.core.db.BaseDao;
import com.jhws.util.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RoomService {
    @Resource
    private BaseDao BaseDao;
    @Resource
    private ITblBuildingDimService buildingDimService;

    /**
     * 保存房间信息
     *
     * @param room 房间信息
     */
    public void saveRoom(Room room) {
        if (room.getCreationTime() == null) {
            room.setCreationTime(new Date());
        }
        BaseDao.saveOrUpdate(room);
    }

    /**
     * 保存房间信息
     *
     * @param building 楼栋信息
     */
    public void saveRoomsByBuilding(TblBuildingDimBo building) throws Exception {
        StringBuilder sql = new StringBuilder("INSERT INTO room (id, comm_code, building_code, unit_code, floor_code, room_code, creation_time) VALUES ");
        String date = DateUtil.getTimeNowFmtSec();
        String appendZeroBuilding = AccountsUtils.appendZeroBuilding(building.getAreaName());
        String appendZeroUnit = AccountsUtils.appendZeroBuilding(building.getBuildingName());
        for (int i = 1; i <= building.getUnitNumber(); i++) {
            for (int j = 1; j <= building.getRoomNumber(); j++) {
                String appendZeroFloor = AccountsUtils.appendZeroBuilding(String.valueOf(i));
                String appendZeroRoom = AccountsUtils.appendZeroBuilding(String.valueOf(j));
                sql.append("('")
                        .append(building.getCommCode()).append(appendZeroBuilding).append(appendZeroUnit).append(appendZeroFloor).append(appendZeroRoom)
                        .append("','").append(building.getCommCode())
                        .append("','").append(appendZeroBuilding)
                        .append("','").append(appendZeroUnit)
                        .append("','").append(appendZeroFloor)
                        .append("','").append(appendZeroRoom)
                        .append("','").append(date)
                        .append("'),");
            }
        }
        sql.deleteCharAt(sql.length() - 1);
        deleteRoom("where comm_code='" + building.getCommCode() + "' and building_code='" + AccountsUtils.appendZeroBuilding(building.getAreaName()) + "' and unit_code='" + AccountsUtils.appendZeroBuilding(building.getBuildingName()) + "'");
        BaseDao.executeBySql(sql.toString());
    }

    /**
     * 查询房间信息
     *
     * @param id 房间信息ID
     */
    public Room queryRoom(String id) {
        return BaseDao.get(Room.class, id);
    }

    /**
     * 条件查询房间信息列表
     *
     * @param where 条件
     */
    public List<Room> queryRooms(String where) {
        return BaseDao.findList("from Room " + where);
    }

    /**
     * 分页查询房间信息列表
     *
     * @param param 条件
     */
    public Map queryRoomPage(Map param) throws Exception {
        String hql = "from Room as r where 1=1 ";
        String commCode = MapUtils.getString(param, "commCode", "");
        if (StringUtils.isNotEmpty(commCode)) {
            hql += " and r.commCode = '" + commCode + "' ";
        }
        String buildingCode = MapUtils.getString(param, "buildingCode", "");
        if (StringUtils.isNotEmpty(buildingCode)) {
            hql += " and r.buildingCode = '" + buildingCode + "'";
        }
        String unitCode = MapUtils.getString(param, "unitCode", "");
        if (StringUtils.isNotEmpty(unitCode)) {
            hql += " and r.unitCode = '" + unitCode + "'";
        }
        String floorCode = MapUtils.getString(param, "floorCode", "");
        if (StringUtils.isNotEmpty(floorCode)) {
            hql += " and r.floorCode = '" + floorCode + "'";
        }
        String roomCode = MapUtils.getString(param, "roomCode", "");
        if (StringUtils.isNotEmpty(roomCode)) {
            hql += " and r.roomCode = '" + roomCode + "' ";
        }
        String roomTypeId = MapUtils.getString(param, "roomTypeId", "");
        if (StringUtils.isNotEmpty(roomTypeId)) {
            hql += " and r.roomTypeId = '" + roomTypeId + "' ";
        }
        Map map = BaseDao.queryPage(hql);
        List<Room> rooms = (List<Room>) map.get("list");
        List<RoomBo> roomBos = new ArrayList<>();
        RoomBo roomBo;
        for (Room room : rooms) {
            roomBo = new RoomBo();
            BeanUtils.copyProperties(roomBo, room);// 属性拷贝
            int iBuilding = Integer.valueOf(room.getBuildingCode());
            int iUnit = Integer.valueOf(room.getUnitCode());
            String where = "where comm_code = '" + room.getCommCode() + "' and area_name = '" + iBuilding + "' and building_name = '" + iUnit + "' ";
            List<TblBuildingDimBo> list = buildingDimService.findTblBuildingDimByWhere(where);
            if (list != null && list.size() > 0) {
                roomBo.setCommName(list.get(0).getCommName());
                roomBo.setBuildingAliasName(list.get(0).getAreaAliasName());
                roomBo.setUnitAliasName(list.get(0).getBuildingAliasName());
            }
            roomBos.add(roomBo);
        }
        map.put("list", roomBos);
        return map;
    }

    /**
     * 分配戶型
     *
     * @param roomId
     * @param roomTypeId
     */
    public void saveRoomType(String roomId, long roomTypeId) {
        Room room = queryRoom(roomId);
        room.setRoomTypeId(roomTypeId);
        saveRoom(room);
    }

    /**
     * 删除房间信息
     *
     * @param room 房间信息ID
     */
    public void deleteRoom(Room room) {
        BaseDao.delete(room);
    }

    /**
     * 条件删除房间信息
     *
     * @param where 条件
     */
    public void deleteRoom(String where) throws Exception {
        String sql = "delete from room " + where;
        BaseDao.executeBySql(sql);
    }
}
